
local ServerOHOH = _G.ServerOHOH;
local M = ServerOHOH.class.new(script, ServerOHOH.files.BaseComponent(),script.Parent.Name);
local MarketplaceService = game:GetService("MarketplaceService")

local Goods_Cfg=ServerOHOH.config.Goods

function M.new(self,...)
	return M(self,...);
end;

function M.ctor(self, parent)
	M.super.ctor(self, parent);
	self.dataBase=self.parent:GetComponent(ServerOHOH.load.Server_PlayerDataComponent())
	self.userId=self.parent:GetUserId()

    self.UsedGame={} --记录已经使用的gamePass，保证GamePass 只被创建一次

    self.GamePassData=self:GetAllGamePass()

    self.parent.UserDataEvent:Connect(function(key)
        if key=="gamePass" then
            self:InitGamePass(true)
        end
    end)

    --检测是否在网页中有购买
    for passKey,v in pairs( self.GamePassData) do
		if not self.dataBase:GetData("gamePass")[passKey] then
			if self:IsHadGamePass(v.pid) then
                self.dataBase:GetData("gamePass")[passKey]=true
			end
		end
	end



    self:InitGamePass()

end;

function M.GetAllGamePass(self)
    local data={}
    for  k,v in pairs(Goods_Cfg) do
        if v.gType=="gamePass" then
            for key,pv in pairs(v.itemData) do
                data[key]={
                    pid=v.pid,
                    value=pv
                }
            end
        end
    end
    return data
end

--判断是否拥有GamePass
function M.IsHadGamePass(self,passId)
	local hasPass = false
	local success, message = pcall(function()
		hasPass = MarketplaceService:UserOwnsGamePassAsync(self.userId, passId)
	end)

	if not success then
		warn("Error while checking if player has pass: " .. tostring(message))
		return false
	end

	if hasPass then
		return true
	else
		return false
	end
end

-- 初始化通行证
function M.InitGamePass(self,...)

    local list={}
    for k,v in pairs(self.dataBase:GetData("gamePass").__real) do
        table.insert(list,k)
    end
    self:UseGamePass(list,...)
    print("use")
end

--让gamePass起作用
function M.UseGamePass(self,gamePassList,...)

    for _,key in pairs(gamePassList) do

        if  self.UsedGame[key] then --已经创建过，继续
            continue
        end

        if M[key] then
            M[key](self,self.GamePassData[key],...)
        end

        self.UsedGame[key]=true --记录已经使用

    end
end

local Game_Cfg=ServerOHOH.config.Game


--永久幸运+2
function M.lucky_roll(self,passData,isUpdate)
    self.RngDataCtrl=self.parent:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    self.RngDataCtrl:AddLucky(passData.value,true)
end

--永久幸运+4
function M.super_lucky_roll(self,passData,isUpdate)
    self.RngDataCtrl=self.parent:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    self.RngDataCtrl:AddLucky(passData.value,true)
end

--shinyluck+10%
function M.shiny_hunter(self,passData,isUpdate)
    self.RngDataCtrl=self.parent:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    self.RngDataCtrl:AddShinyLucky(passData.value,true)
end

--一次roll 产生2个结果
function M.double_roll(self,passData,isUpdate)
    self.parent.RollDropMulEvent:Connect(function()
        return passData.value
    end)
end

--双倍钻石
function M.double_diamond(self,passData,isUpdate)
    self.parent.DiamondDropMulEvent:Connect(function()
        return passData.value
    end)
end

--AFK中+1个lucky;+3% shinyLucky;VIPTag（聊天）
function M.VIP(self,passData,isUpdate)
    self.RngDataCtrl=self.parent:GetComponent(_G.CommonOHOH.load.Common_RngDataLayer())
    self.RngDataCtrl:AddShinyLucky(passData.value["shinyLuck"],true)
end



--增加宠物装备数量通行证1
function M.gamepass_pet_equip1(self,passData,isUpdate)
    -- local data=self.dataBase:GetData("petsPack")

    -- if data["equipMax"]<Game_Cfg.BuyEquip[1].count then
    --     data["equipMax"]=data["equipMax"]+ passData.value
    -- end
    --   if isUpdate then
    --     self.parent:SynchDataToClient("petsPack")
    -- end
  
end


--增加宠物装备数量通行证2
function M.gamepass_pet_equip2(self,passData,isUpdate)
    local data=self.dataBase:GetData("petsPack")
    if data["equipMax"]<Game_Cfg.BuyEquip[2].count then
        data["equipMax"]=data["equipMax"]+ passData.value
    end
  
    if isUpdate then
        self.parent:SynchDataToClient("petsPack")
    end
end
--增加宠物背包数量通行证1
function M.gamepass_pet_pack1(self,passData,isUpdate)
    local data=self.dataBase:GetData("petsPack")
    if data["backpack"]<(Game_Cfg.backpack+passData.value) then
        data["backpack"]=data["backpack"]+ passData.value
    end
    if isUpdate then
        self.parent:SynchDataToClient("petsPack")
    end
end

--宠物一叶草
function M.gamepass_pet_luck1(self,passData,isUpdate)
    self.parent.PetLuckyEvent:Connect(function()
        return passData.value
    end)
end

--宠物二叶草
function M.gamepass_pet_luck2(self,passData,isUpdate)
    self.parent.PetLuckyEvent:Connect(function()
        return passData.value
    end)
end

--宠物三叶草
function M.gamepass_pet_luck3(self,passData,isUpdate)
    self.parent.PetLuckyEvent:Connect(function()
        return passData.value
    end)
end

--摇蛋数量增加通行证1
function M.gamepass_hatch_egg1(self,passData,isUpdate)
    self.parent.PetLuckyEvent:Connect(function()
        return passData.value
    end)
end


--摇蛋数量增加通行证2
function M.gamepass_hatch_egg2(self,passData,isUpdate)
    self.parent.PetLuckyEvent:Connect(function()
        return passData.value
    end)
end

--通行证礼包1
function M.gamepass_bundle1(self,passData,isUpdate)
    local list=   string.split(passData.value,",")
    for _,v in pairs(list) do
        self.dataBase:GetData("gamePass")[v]=true
    end
    self:UseGamePass(list)
end

--通行证礼包2
function M.gamepass_bundle2(self,passData,isUpdate)
    local list=   string.split(passData.value,",")
    for _,v in pairs(list) do
        self.dataBase:GetData("gamePass")[v]=true
    end
    self:UseGamePass(list)
end

--购买了ViP
function M.gamepass_vip(self,passData,isUpdate)
    local list=   string.split(passData.value,",")
    for _,v in pairs(list) do
        self.dataBase:GetData("gamePass")[v]=true
    end
    self:UseGamePass(list)
end



--黄金宠物减少
function M.gamepass_make_gold(self,passData,isUpdate)
    self.parent.MakeGoldPetCountEvent:Connect(function()
        return passData.value
    end)
end

--虚空1
function M.gamepass_void_time1(self,passData,isUpdate)
    self.parent.MakeVoidPetTimeScalEvent:Connect(function()
        return passData.value
    end)
end

--虚空2
function M.gamepass_void_time2(self,passData,isUpdate)
    self.parent.MakeVoidPetTimeScalEvent:Connect(function()
        return passData.value
    end)
end

function M.dtor(self)
	M.super.dtor(self);
end;



return M
